package com.jiangtour.controller;

import com.jiangtour.dataobject.ConfigureCategory;
import com.jiangtour.dataobject.ProductCategory;
import com.jiangtour.dataobject.SiteInfo;
import com.jiangtour.exception.SiteException;
import com.jiangtour.form.CategoryForm;
import com.jiangtour.form.ConfigureForm;
import com.jiangtour.service.ConfigureCategoryService;
import com.jiangtour.service.SiteInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * @Title: SiteConfigureController
 * @ProjectName site
 * @Description: 站点配置
 * @date 2018/12/24/02417:46
 */
@Controller
@RequestMapping("/site/configure")
public class SiteConfigureController {
    @Autowired
    private ConfigureCategoryService configureCategoryService;
    @Autowired
    private SiteInfoService siteInfoService;




    /**
     * 配置列表
     * @param map
     * @return
     */
    @GetMapping("/list")
    public ModelAndView list(@RequestParam("type") Integer type, HttpServletRequest request, Map<String, Object> map) {
        SiteInfo currentAccount = siteInfoService.findCurrentAccount(request);
        List<ConfigureCategory> list = configureCategoryService.findList(currentAccount.getId(), type);
        map.put("configureList", list);
        return new ModelAndView("configure/list", map);
    }


    @GetMapping("/index")
    public ModelAndView index(@RequestParam(value = "categoryId", required = false) Integer categoryId,
                              @RequestParam(value = "configType", required = false) Integer configType,
                              Map<String, Object> map) {

        if (categoryId !=null ){
            ConfigureCategory configureCategory = configureCategoryService.findOne(categoryId);
            map.put("configure", configureCategory);
            map.put("categoryType",configureCategory.getCategoryType());
        }else {
            map.put("categoryType",configType);
        }

        return  new ModelAndView("configure/index", map);
    }


    /**
     * 保存/更新
     * @param form
     * @param bindingResult
     * @param map
     * @return
     */
    @PostMapping("/save")
    public ModelAndView save(@Valid ConfigureForm form,
                             BindingResult bindingResult,HttpServletRequest request,
                             Map<String, Object> map) {

        // 归属
        SiteInfo currentAccount = siteInfoService.findCurrentAccount(request);

        if(bindingResult.hasErrors()){
            map.put("msg", bindingResult.getFieldError().getDefaultMessage());
            map.put("url", "/site/category/index");
            return new ModelAndView("common/error", map);
        }
        ConfigureCategory configureCategory = new ConfigureCategory();

        try {
            if(form.getCategoryId() !=null ){
                configureCategory = configureCategoryService.findOne(form.getCategoryId());
            }
            BeanUtils.copyProperties(form, configureCategory);
            configureCategory.setCategoryAscription(currentAccount.getId());
            configureCategoryService.save(configureCategory);
        }catch (SiteException e){
            map.put("msg", e.getMessage());
            map.put("url", "/site/configure/index");
            return new ModelAndView("common/error", map);
        }

        map.put("url", "/site/configure/list?type="+configureCategory.getCategoryType());
        return new ModelAndView("common/success", map);
    }


    @GetMapping("/service")
    public ModelAndView serviceAmount( HttpServletRequest request, Map<String, Object> map) {
        SiteInfo currentAccount = siteInfoService.findCurrentAccount(request);
        if(currentAccount !=null ){
              map.put("serviceAmount", currentAccount.getServiceAmount());
              map.put("siteId",currentAccount.getId());
        }

        return  new ModelAndView("/configure/service", map);
    }

    @GetMapping("/update")
    public ModelAndView updateAmount(@RequestParam(value = "serviceAmount", required = true) BigDecimal serviceAmount,
                                      HttpServletRequest request,
                                      Map<String, Object> map) {
        SiteInfo currentAccount = siteInfoService.findCurrentAccount(request);

        try {
            if(currentAccount !=null ){
                currentAccount.setServiceAmount(serviceAmount);
            }
            siteInfoService.create(currentAccount);
        }catch (SiteException e){
            map.put("msg", e.getMessage());
            map.put("url", "/site/configure/service");
            return new ModelAndView("common/error", map);
        }
        map.put("url", "/site/configure/service");
        return new ModelAndView("common/success", map);
    }

    /**
     * 高德地图
     */
    @RequestMapping("map")
    public String map() {
        return "redirect:/commonModule/map.jsp";
    }


}
